package org.massdeception.gcal.notifytracker.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class NotifyTrackerConfig
{
    public static final String CONFIG_ACCOUNT_NAME = "notify-tracker.gcal.account";
    
    public static final String CONFIG_ACCOUNT_PASSWORD = "notify-tracker.gcal.password";
    
    public static final String CONFIG_GCAL_SERVICE_URL = "notify-tracker.gcal.service.url";
    
    public static final String CONFIG_GCAL_EXCLUDE = "notify-tracker.gcal.events.exclude";
    
    public static final String CONFIG_GCAL_CHECK_INTERVAL = "notify-tracker.gcal.check-interval";
    
    public static final String CONFIG_GCAL_PROXY_HOST = "notify-tracker.proxy.host";
    
    public static final String CONFIG_GCAL_PROXY_PORT = "notify-tracker.proxy.port";
    
    private static final String PROPERTIES_FILE = "notify-tracker.properties";

    private static NotifyTrackerConfig _instance = new NotifyTrackerConfig();

    private Properties _props = null;

    private static boolean failed = false;

    public static NotifyTrackerConfig getConfiguration()
    {
        return (_instance);
    }

    private NotifyTrackerConfig()
    {
        InputStream in = null;
        _props = new Properties();
        try
        {
            String homeDir = System.getProperty("user.dir");
            in = new FileInputStream(homeDir+File.separator+PROPERTIES_FILE);
            if (in == null)
                failed = true;
            _props.load(in);
        } 
        catch (Throwable t)
        {
            failed = true;
        }
    }

    public String getProperty(String name, String defValue)
    {
        
        if (!failed)
        {
            String value = null;
            if ( defValue == null )
                value = _props.getProperty(name);
            else
                value = _props.getProperty(name, defValue);
            if (value == null)
                throw new RuntimeException("Could not retrieve value for "
                        + name + " from properties file.");
            return (value);
        } else
            throw new RuntimeException("Could not load properties file "
                    + PROPERTIES_FILE
                    + " please ensure the file is in your home directory.");
    }
    
    public String getProperty(String name)
    {
        return(getProperty(name, null));
    }

    public synchronized static void reset()
    {
        _instance = new NotifyTrackerConfig();
    }
}